/********************************************************************************
  * @Copyright: Metanergy Technology R&D Co., Ltd
  * @filename: myg0025_flash.h
  * @brief: FLASH Initialization and Configuration functions
  * @Author: AE Team
  * @version: V1.0.0/2022-05-24
*********************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MYG0025_FLASH_H
#define __MYG0025_FLASH_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "myg0025.h"

/** @addtogroup FLASH FLASH
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/**
  * @brief  FLASH Status
  */
typedef enum
{
    FLASH_BUSY = 1,
    FLASH_ERROR_WRP,
    FLASH_ERROR_PROGRAM,
    FLASH_COMPLETE,
    FLASH_TIMEOUT
} FLASH_Status;

/**
  * @brief Option Bytes Registers
  */
typedef struct
{
    __IO uint32_t RDP;          /*!< FLASH option byte Read protection,             Address offset: 0x00 */
    __IO uint32_t USER;         /*!< FLASH option byte user options,                Address offset: 0x02 */
    __IO uint32_t DATA0;        /*!< User data byte 0 (stored in FLASH_OBR[23:16]), Address offset: 0x04 */
    __IO uint32_t DATA1;        /*!< User data byte 1 (stored in FLASH_OBR[31:24]), Address offset: 0x06 */
    __IO uint32_t WRP01;         /*!< FLASH option byte write protection 01,          Address offset: 0x08 */
    __IO uint32_t WRP23;         /*!< FLASH option byte write protection 23,          Address offset: 0x0C */
} OB_TypeDef;



/* Exported constants --------------------------------------------------------*/

/** @defgroup FLASH_Exported_Constants FLASH_Exported_Constants
  * @{
  */

/** @defgroup  FLASH_OB_BASE_UID_KEY_definition FLASH_OB_BASE_UID_KEY_definition
  * @{
  */

#define OB_BASE             ((uint32_t)0x1FFFF800U)       /*!< FLASH Option Bytes base address */
#define OB                  ((OB_TypeDef *) OB_BASE)
#define OB_RDP_REG                  *(uint32_t *)(OB_BASE + 0x00) /* FLASH Option Bytes RDP address */
#define OB_USER_REG                 *(uint32_t *)(OB_BASE + 0x00) /* FLASH Option Bytes USR address */
#define OB_DATA0_REG                    *(uint32_t *)(OB_BASE + 0x04) /* FLASH Option Bytes DATA0 address */
#define OB_DATA1_REG                    *(uint32_t *)(OB_BASE + 0x04) /* FLASH Option Bytes DATA1 address */
#define OB_WRP01_REG                    *(uint32_t *)(OB_BASE + 0x08) /* FLASH Option Bytes WRP01 address */
#define OB_WRP23_REG                    *(uint32_t *)(OB_BASE + 0x0C) /* FLASH Option Bytes WRP23 address */


#define UID_BASE            ((uint32_t)0x1FFFF924)       /* UID base address */

#define FLASH_KEY1  ((uint32_t)(0x45670123U ))
#define FLASH_KEY2  ((uint32_t)(0xCDEF89ABU ))

#define FLASH_OPTKEY1  ((uint32_t)(0x45670123U ))
#define FLASH_OPTKEY2  ((uint32_t)(0xCDEF89ABU ))

/**
  * @}
  */

/** @defgroup FLASH_Latency FLASH_Latency
  * @{
  */
#define FLASH_Latency_0                ((uint32_t)0x00000000)  /*!< FLASH Zero Latency cycle */
#define FLASH_Latency_1                ((uint32_t)0x00000001)  /*!< FLASH One Latency cycle */
#define FLASH_Latency_2                ((uint32_t)0x00000002)  /*!< FLASH TWO Latency cycles */
#define FLASH_Latency_3                ((uint32_t)0x00000003)  /*!< FLASH THREE Latency cycles */
#define FLASH_Latency_4                ((uint32_t)0x00000004)  /*!< FLASH FOUR Latency cycles */
#define FLASH_Latency_5                ((uint32_t)0x00000005)  /*!< FLASH FIVE Latency cycles */
#define FLASH_Latency_6                ((uint32_t)0x00000006)  /*!< FLASH SIX Latency cycles */
#define FLASH_Latency_7                ((uint32_t)0x00000007)  /*!< FLASH SEVEN Latency cycles */
#define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
                                   ((LATENCY) == FLASH_Latency_1) || \
                                   ((LATENCY) == FLASH_Latency_2) || \
                                   ((LATENCY) == FLASH_Latency_3) || \
                                   ((LATENCY) == FLASH_Latency_4) || \
                                   ((LATENCY) == FLASH_Latency_5) || \
                                   ((LATENCY) == FLASH_Latency_6) || \
                                   ((LATENCY) == FLASH_Latency_7))
/**
  * @}
  */

/** @defgroup FLASH_Interrupts FLASH_Interrupts
  * @{
  */

#define FLASH_IT_EOP                   FLASH_CR_EOPIE  /*!< End of programming interrupt source */
#define FLASH_IT_ERR                   FLASH_CR_ERRIE  /*!< Error interrupt source */
#define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
/**
  * @}
  */

/** @defgroup FLASH_Address FLASH_Address
  * @{
  */
#define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))

/**
  * @}
  */

/** @defgroup FLASH_Option_Bytes_Write_Protection FLASH_Option_Bytes_Write_Protection
  * @{
  */

#define OB_WRP_Pages0to7               ((uint32_t)0x00000001) /* Write protection of page 0 to 7 */
#define OB_WRP_Pages8to15               ((uint32_t)0x00000002) /* Write protection of page 8 to 15 */
#define OB_WRP_Pages16to23              ((uint32_t)0x00000004) /* Write protection of page 16 to 23 */
#define OB_WRP_Pages24to31             ((uint32_t)0x00000008) /* Write protection of page 24 to 31 */
#define OB_WRP_Pages32to39             ((uint32_t)0x00000010) /* Write protection of page 32 to 39 */
#define OB_WRP_Pages40to47             ((uint32_t)0x00000020) /* Write protection of page 40 to 47 */
#define OB_WRP_Pages48to55             ((uint32_t)0x00000040) /* Write protection of page 48 to 55 */
#define OB_WRP_Pages56to63             ((uint32_t)0x00000080) /* Write protection of page 56 to 63 */
#define OB_WRP_Pages64to71             ((uint32_t)0x00000100) /* Write protection of page 64 to 71 */
#define OB_WRP_Pages72to79             ((uint32_t)0x00000200) /* Write protection of page 72 to 79 */
#define OB_WRP_Pages80to87             ((uint32_t)0x00000400) /* Write protection of page 80 to 87 */
#define OB_WRP_Pages88to95             ((uint32_t)0x00000800) /* Write protection of page 88 to 95 */
#define OB_WRP_Pages96to103             ((uint32_t)0x00001000) /* Write protection of page 96 to 103 */
#define OB_WRP_Pages104to111             ((uint32_t)0x00002000) /* Write protection of page 104 to 111 */
#define OB_WRP_Pages112to119             ((uint32_t)0x00004000) /* Write protection of page 112 to 119 */
#define OB_WRP_Pages120to127             ((uint32_t)0x00008000) /* Write protection of page 120 to 127 */
#define OB_WRP_Pages128to135             ((uint32_t)0x00010000) /* Write protection of page 128 to 135 */
#define OB_WRP_Pages136to143             ((uint32_t)0x00020000) /* Write protection of page 136 to 143 */
#define OB_WRP_Pages144to151             ((uint32_t)0x00040000) /* Write protection of page 144 to 151 */
#define OB_WRP_Pages152to159             ((uint32_t)0x00080000) /* Write protection of page 152 to 159 */
#define OB_WRP_Pages160to167             ((uint32_t)0x00100000) /* Write protection of page 160 to 167 */
#define OB_WRP_Pages168to175             ((uint32_t)0x00200000) /* Write protection of page 168 to 175 */
#define OB_WRP_Pages176to183             ((uint32_t)0x00400000) /* Write protection of page 176 to 183 */
#define OB_WRP_Pages184to191             ((uint32_t)0x00800000) /* Write protection of page 184 to 191 */
#define OB_WRP_Pages192to199             ((uint32_t)0x01000000) /* Write protection of page 192 to 199 */
#define OB_WRP_Pages200to207           ((uint32_t)0x02000000) /* Write protection of page 200 to 207 */
#define OB_WRP_Pages208to215           ((uint32_t)0x04000000) /* Write protection of page 208 to 215 */
#define OB_WRP_Pages216to223           ((uint32_t)0x08000000) /* Write protection of page 216 to 223 */
#define OB_WRP_Pages224to231           ((uint32_t)0x10000000) /* Write protection of page 224 to 231 */
#define OB_WRP_Pages232to239           ((uint32_t)0x20000000) /* Write protection of page 232 to 239 */
#define OB_WRP_Pages240to247           ((uint32_t)0x40000000) /* Write protection of page 240 to 247 */
#define OB_WRP_Pages248to255           ((uint32_t)0x80000000) /* Write protection of page 248 to 255 */

#define OB_WRP_AllPages                ((uint32_t)0xFFFFFFFF) /*!< Write protection of all Sectors */

#define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))

#define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))

/**
  * @}
  */

/** @defgroup FLASH_Option_Bytes_Read_Protection FLASH_Option_Bytes_Read_Protection
  * @{
  */

/**
  * @brief  FLASH_Read Protection Level
  */
#define OB_RDP_Level_0   ((uint32_t)0x00FF55AA)
#define OB_RDP_Level_1   ((uint32_t)0x00FF44BB)
#define OB_RDP_Level_2   ((uint32_t)0x00FF33CC) /* Warning: When enabling read protection level 2 
                                                it's no more possible to go back to level 1 or 0 */

#define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
                          ((LEVEL) == OB_RDP_Level_1)||\
                          ((LEVEL) == OB_RDP_Level_2))
/**
  * @}
  */

/** @defgroup FLASH_Option_Bytes_IWatchdog FLASH_Option_Bytes_IWatchdog
  * @{
  */

#define OB_IWDG_SW                     ((uint8_t)0x01)  /*!< Software IWDG selected */
#define OB_IWDG_HW                     ((uint8_t)0x00)  /*!< Hardware IWDG selected */
#define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))

/**
  * @}
  */

/** @defgroup FLASH_Option_Bytes_nRST_STOP FLASH_Option_Bytes_nRST_STOP
  * @{
  */

#define OB_STOP_NoRST                  ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
#define OB_STOP_RST                    ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
#define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))

/**
  * @}
  */

/** @defgroup FLASH_Flags FLASH_Flags
  * @{
  */

#define FLASH_FLAG_BSY                 FLASH_SR_BSY     /*!< FLASH Busy flag */
#define FLASH_FLAG_PGERR               FLASH_SR_PGERR   /*!< FLASH Programming error flag */
#define FLASH_FLAG_WRPERR              FLASH_SR_WRPRTERR  /*!< FLASH Write protected error flag */
#define FLASH_FLAG_EOP                 FLASH_SR_EOP     /*!< FLASH End of Programming flag */

#define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFBCA) == 0x00000000) && ((FLAG) != 0x00000000))

#define IS_FLASH_GET_FLAG(FLAG)  (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || ((FLAG) == FLASH_FLAG_PGERR) || \
                                  ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
/**
  * @}
  */

/** @defgroup FLASH_Timeout_definition FLASH_Timeout_definition
  * @{
  */
#define FLASH_ER_PRG_TIMEOUT         ((uint32_t)0x000B0000)

/**
  * @}
  */

/** @defgroup  FLASH_Option_Bytes_RDP_USER_WRP_definition FLASH_Option_Bytes_RDP_USER_WRP_definition
  * @{
  */

/******************  Bit definition for OB_RDP register  **********************/
#define OB_RDP_RDP_Pos       (0U)
#define OB_RDP_RDP_Msk       (0xFFU << OB_RDP_RDP_Pos)                         /*!< 0x000000FF */
#define OB_RDP_RDP           OB_RDP_RDP_Msk                                    /*!< Read protection option byte */
#define OB_RDP_nRDP_Pos      (8U)
#define OB_RDP_nRDP_Msk      (0xFFU << OB_RDP_nRDP_Pos)                        /*!< 0x0000FF00 */
#define OB_RDP_nRDP          OB_RDP_nRDP_Msk                                   /*!< Read protection complemented option byte */

/******************  Bit definition for OB_USER register  *********************/
#define OB_USER_USER_Pos     (16U)
#define OB_USER_USER_Msk     (0xFFU << OB_USER_USER_Pos)                       /*!< 0x00FF0000 */
#define OB_USER_USER         OB_USER_USER_Msk                                  /*!< User option byte */
#define OB_USER_nUSER_Pos    (24U)
#define OB_USER_nUSER_Msk    (0xFFU << OB_USER_nUSER_Pos)                      /*!< 0xFF000000 */
#define OB_USER_nUSER        OB_USER_nUSER_Msk                                 /*!< User complemented option byte */

/******************  Bit definition for OB_WRP0 register  *********************/
#define OB_WRP0_WRP0_Pos     (0U)
#define OB_WRP0_WRP0_Msk     (0xFFU << OB_WRP0_WRP0_Pos)                       /*!< 0x000000FF */
#define OB_WRP0_WRP0         OB_WRP0_WRP0_Msk                                  /*!< Flash memory write protection option bytes */
#define OB_WRP0_nWRP0_Pos    (8U)
#define OB_WRP0_nWRP0_Msk    (0xFFU << OB_WRP0_nWRP0_Pos)                      /*!< 0x0000FF00 */
#define OB_WRP0_nWRP0        OB_WRP0_nWRP0_Msk                                 /*!< Flash memory write protection complemented option bytes */

/**
  * @}
  */

/** @defgroup FLASH_Legacy FLASH_Legacy
  * @{
  */
#define FLASH_WRProt_Pages0to7         OB_WRP_Pages0to7
#define FLASH_WRProt_Pages8to15        OB_WRP_Pages8to15
#define FLASH_WRProt_Pages16to23           OB_WRP_Pages16to23
#define FLASH_WRProt_Pages24to31         OB_WRP_Pages24to31
#define FLASH_WRProt_Pages32to39         OB_WRP_Pages32to39
#define FLASH_WRProt_Pages40to47             OB_WRP_Pages40to47
#define FLASH_WRProt_Pages48to55         OB_WRP_Pages48to55
#define FLASH_WRProt_Pages56to63         OB_WRP_Pages56to63
#define FLASH_WRProt_Pages64to71             OB_WRP_Pages64to71
#define FLASH_WRProt_Pages72to79             OB_WRP_Pages72to79
#define FLASH_WRProt_Pages80to87         OB_WRP_Pages80to87
#define FLASH_WRProt_Pages88to95         OB_WRP_Pages88to95
#define FLASH_WRProt_Pages96to103        OB_WRP_Pages96to103
#define FLASH_WRProt_Pages104to111       OB_WRP_Pages104to111
#define FLASH_WRProt_Pages112to119       OB_WRP_Pages112to119
#define FLASH_WRProt_Pages120to127       OB_WRP_Pages120to127
#define FLASH_WRProt_Pages128to135       OB_WRP_Pages128to135
#define FLASH_WRProt_Pages136to143       OB_WRP_Pages136to143
#define FLASH_WRProt_Pages144to151       OB_WRP_Pages144to151
#define FLASH_WRProt_Pages152to159       OB_WRP_Pages152to159
#define FLASH_WRProt_Pages160to167       OB_WRP_Pages160to167
#define FLASH_WRProt_Pages168to175       OB_WRP_Pages168to175
#define FLASH_WRProt_Pages176to183       OB_WRP_Pages176to183
#define FLASH_WRProt_Pages184to191       OB_WRP_Pages184to191
#define FLASH_WRProt_Pages192to199       OB_WRP_Pages192to199
#define FLASH_WRProt_Pages200to207     OB_WRP_Pages200to207
#define FLASH_WRProt_Pages208to215     OB_WRP_Pages208to215
#define FLASH_WRProt_Pages216to223     OB_WRP_Pages216to223
#define FLASH_WRProt_Pages224to231     OB_WRP_Pages224to231
#define FLASH_WRProt_Pages232to239     OB_WRP_Pages232to239
#define FLASH_WRProt_Pages240to247     OB_WRP_Pages240to247
#define FLASH_WRProt_Pages248to255     OB_WRP_Pages248to255

#define FLASH_WRProt_AllPages          OB_WRP_AllPages
/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

/** @defgroup FLASH_Exported_Functions FLASH_Exported_Functions
  * @{
  */

/**
  * @brief  FLASH memory functions that can be executed from FLASH.
  */
/* FLASH Interface configuration functions ************************************/
void FLASH_SetLatency(uint32_t FLASH_Latency);
void FLASH_PrefetchBufferCmd(FunctionalState NewState);
FlagStatus FLASH_GetPrefetchBufferStatus(void);

/* FLASH Memory Programming functions *****************************************/
void FLASH_Unlock(void);
void FLASH_Lock(void);
FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
FLASH_Status FLASH_EraseAllPages(void);
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);

/* FLASH Option Bytes Programming functions *****************************************/
void FLASH_OB_Unlock(void);
void FLASH_OB_Lock(void);
void FLASH_OB_Launch(void);
FLASH_Status FLASH_OB_Erase(void);
FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
FLASH_Status FLASH_OB_RDPConfig(uint32_t OB_RDP);
FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP);
FLASH_Status FLASH_OB_User_RDP_Config(uint8_t OB_IWDG, uint8_t OB_STOP, uint32_t OB_RDP);
FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
FLASH_Status FLASH_OB_BOOT0Config(uint8_t OB_BOOT0);
FLASH_Status FLASH_OB_BOOT0SWConfig(uint8_t OB_BOOT0SW);
FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity);
FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
FLASH_Status FLASH_OB_ProgramData(uint32_t Address);
FLASH_Status FLASH_OB_Program(uint32_t Address, uint32_t Data);
uint8_t FLASH_OB_GetUser(void);
uint32_t FLASH_OB_GetWRP(void);
FlagStatus FLASH_OB_GetRDP(void);

/* FLASH Interrupts and flags management functions **********************************/
void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
void FLASH_ClearFlag(uint32_t FLASH_FLAG);
FLASH_Status FLASH_GetStatus(void);
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
void Sys_GetDevice96BitUID(uint32_t *UID);
/** @defgroup FLASH_Legacy FLASH_Legacy
  * @{
  */
#define FLASH_EraseOptionBytes               FLASH_OB_Erase
#define FLASH_EnableWriteProtection          FLASH_OB_EnableWRP
#define FLASH_UserOptionByteConfig           FLASH_OB_UserConfig
#define FLASH_ProgramOptionByteData          FLASH_OB_ProgramData
#define FLASH_GetUserOptionByte              FLASH_OB_GetUser
#define FLASH_GetWriteProtectionOptionByte   FLASH_OB_GetWRP

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __MYG0025_FLASH_H */


